﻿Public Class frmSnake

    Private Sub frmSnake_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mySnake.IniciarSnake()

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        lblFruit.Visible = True

        Select Case e.KeyValue
            Case Keys.Right
                If x < MaxRight And derecha = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = True
                    izquierda = False
                    derecha = True
                    arriba = True
                    abajo = True
                ElseIf x > MaxLeft And izquierda = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = True
                    TimerRigth.Enabled = False
                End If

            Case Keys.Left
                If x >= MaxLeft And izquierda = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = True
                    TimerRigth.Enabled = False
                    izquierda = True
                    derecha = False
                    arriba = True
                    abajo = True
                ElseIf x < MaxRight And derecha = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = True

                End If
            Case Keys.Down

                If y < MaxBottom And abajo = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = True
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = False
                    izquierda = True
                    derecha = True
                    arriba = False
                    abajo = True
                ElseIf arriba = True And y > MaxTop Then
                    Timer1.Enabled = True
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = False
                End If

            Case Keys.Up
                If y > MaxTop And arriba = True Then
                    Timer1.Enabled = True
                    TimerBottom.Enabled = False
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = False
                    izquierda = True
                    derecha = True
                    arriba = True
                    abajo = False
                ElseIf y < MaxBottom And abajo = True Then
                    Timer1.Enabled = False
                    TimerBottom.Enabled = True
                    TimerLeft.Enabled = False
                    TimerRigth.Enabled = False
                End If
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mySnake.añadirCuerpo(New Point(130, 250))

    End Sub


    Public Sub TimerTop_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles Timer1.Tick
        y = y - 10
        If y < 0 Then
            Timer1.Stop()
            Me.volverEmpezar()
        Else
            Me.comprobarCabezaCuerpo()
            Me.comprobarPosicionesFrutaSnake()
        End If

        mySnake.movimiento()


    End Sub

    Public Sub TimerBottom_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles TimerBottom.Tick
        y = y + 10
        If y > 292 Then
            TimerBottom.Stop()
            Me.volverEmpezar()
        Else
            Me.comprobarCabezaCuerpo()
            Me.comprobarPosicionesFrutaSnake()
        End If


        mySnake.movimiento()
    End Sub

    Public Sub TimerRigth_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles TimerRigth.Tick
        x = x + 10
        If x > 320 Then
            TimerRigth.Stop()
            Me.volverEmpezar()
        Else
            Me.comprobarCabezaCuerpo()
            Me.comprobarPosicionesFrutaSnake()

            mySnake.movimiento()
        End If


    End Sub

    Public Sub TimerLeft_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles TimerLeft.Tick
        x = x - 10
        If x < 0 Then
            TimerLeft.Stop()
            Me.volverEmpezar()
        Else
            Me.comprobarCabezaCuerpo()
            Me.comprobarPosicionesFrutaSnake()
            mySnake.movimiento()
        End If


    End Sub

    Private Sub comprobarPosicionesFrutaSnake()
        Dim nuevaPosicion As Point
        If lblFruit.Location = mySnake.cuerpo(0).Location Then
            lblFruit.Location = myfruit.crearFruta(mySnake.cuerpo(0).Location, lblFruit.Location)
            nuevaPosicion = New Point(mySnake.cuerpo(0).Location.X, mySnake.cuerpo(0).Location.Y)
            mySnake.añadirCuerpo(nuevaPosicion)
            myfruit.SumarPuntos()
        End If
    End Sub


    Private Sub volverEmpezar()
        Dim answer As DialogResult
        answer = MessageBox.Show("Has logrado " & puntosTotales & " puntos.", "GAME OVER", MessageBoxButtons.RetryCancel)
        MiJuego.UsuarioActual.AñadirPuntuaciones(1, puntosTotales)
        If answer = Windows.Forms.DialogResult.Retry Then
            For Each ml As Control In Me.Controls
                If ml.GetType = GetType(Label) And ml.BackColor <> Color.LawnGreen Then
                    ml.Visible = False
                    'Me.Controls.Remove(ml)
                End If
            Next
            mySnake.IniciarSnake()
            myfruit.nuevaPosicionFruta()
            arriba = True
            abajo = True
            derecha = True
            izquierda = True
        Else
            Me.Close()

        End If
    End Sub
    Private Sub comprobarCabezaCuerpo()
        For i As Integer = mySnake.cuerpo.Length - 1 To 1 Step -1
            If mySnake.cuerpo(0).Location = mySnake.cuerpo(i).Location Then
                TimerLeft.Stop()
                TimerRigth.Stop()
                TimerBottom.Stop()
                Timer1.Stop()
                Me.volverEmpezar()
                Exit Sub
            End If
        Next
    End Sub

End Class
